package com.though.alipay.handler;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.though.alipay.config.AlipayConfig;
import com.though.alipay.entity.ParamsRefund;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


public class Alipay {
	
	public static Logger logger = LoggerFactory.getLogger(Alipay.class);
	
	private static AlipayClient alipayClient = null;
	
	private static AlipayClient alipayClientRefund = null;
	
	static {
		alipayClient = new DefaultAlipayClient(AlipayConfig.getParams().getGatway(), AlipayConfig.getParams().getAppid(), 
				AlipayConfig.getParams().getPrivateKey(), AlipayConfig.FORMAT, AlipayConfig.ENCODING, AlipayConfig.getParams().getPublicKey(), AlipayConfig.RSA2);
		
		alipayClientRefund = new DefaultAlipayClient(AlipayConfig.getParams().getGatway(), AlipayConfig.getParams().getAppid(), 
				AlipayConfig.getParams().getPrivateKey(), AlipayConfig.FORMAT, AlipayConfig.ENCODING, AlipayConfig.getParams().getAlipublicKey(), AlipayConfig.RSA2);
	}
	
	
	/**
	 * 校验支付宝签名数据
	 */
	public static boolean checkAlipaySignature(Map<String, String> params) throws AlipayApiException {
		return AlipaySignature.rsaCheckV1(params, AlipayConfig.getParams().getAlipublicKey(), AlipayConfig.ENCODING, AlipayConfig.RSA2);
	}
	
	
	/**
	 * 支付请求
	 * @return
	 */
	public static String pay(String body, String subject, String payNo, double amount, String notifyUrl) {
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody(StringUtils.isBlank(body) ? "" : body);
		model.setSubject(subject);
		model.setOutTradeNo(payNo);
		model.setTimeoutExpress("30m");
		model.setTotalAmount(String.valueOf(amount));
		model.setProductCode("QUICK_MSECURITY_PAY");
		request.setBizModel(model);
		request.setNotifyUrl(notifyUrl);
		try {
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
			logger.info("Alipay pay response:{}", response.getBody());
			return response.getBody();
		} catch (AlipayApiException e) {
			logger.error("Error 支付请求，后端发生异常：", e);
			throw new RuntimeException("申请Alipay支付异常！");
		}
	}
	
	
	/**
	 * 退款请求
	 * @return
	 */
	public static String refund(ParamsRefund refund, String notifyUrl) {
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		request.setBizContent(JSON.toJSONString(refund));
		if (StringUtils.isBlank(notifyUrl)) {
			request.setNotifyUrl(notifyUrl);
		}
		try {
			AlipayTradeRefundResponse response = alipayClientRefund.execute(request);
			logger.warn("退款请求返回数据：" + response.getBody());
			return response.getBody();
		} catch (AlipayApiException e) {
			logger.error("Error 退款请求，后端发生异常：", e);
			throw new RuntimeException("退款Alipay异常！");
		}
	}
}
